namespace CSharpePro._1014;

public class Bubble
{
    public void BubbleSort(int[] array)
    {
        int n = array.Length-1;
        bool swapped;
        for (int i = 0; i < n; i++)
        {
            swapped = false;
            for (int j = 0; j < n - i; j++)
            {
                if (array[j] > array[j + 1])
                {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    swapped = true;
                }
            }
            if (!swapped)
                break;
        }
    }
    public void PrintArray(int[] array)
    {
        foreach (int item in array)
        {
            Console.Write(item + " ");
        }
        Console.WriteLine();
    } 
    public void test()
    {
        // 测试数组
        int[] numbers = { 64, 34, 25, 12, 22, 11, 90 };
        
        Console.WriteLine("排序前的数组:");
        PrintArray(numbers);
        
        // 调用冒泡排序
        BubbleSort(numbers);
        
        Console.WriteLine("排序后的数组:");
        PrintArray(numbers);
    }
}